<?php

/*-----------------------------------------------------------------------------------*/
/* Start WooThemes Functions - Please refrain from editing this section */
/*-----------------------------------------------------------------------------------*/

// Set path to WooFramework and theme specific functions
$functions_path = get_template_directory() . '/functions/';
$includes_path = get_template_directory() . '/includes/';

// Define the theme-specific key to be sent to PressTrends.
define( 'WOO_PRESSTRENDS_THEMEKEY', 'ak8088bggwqrvw66acb4evlcdye6t3cq9' );

// WooFramework
require_once ($functions_path . 'admin-init.php' );			// Framework Init

/*-----------------------------------------------------------------------------------*/
/* Load the theme-specific files, with support for overriding via a child theme.
/*-----------------------------------------------------------------------------------*/

$includes = array(
				'includes/theme-options.php', 			// Options panel settings and custom settings
				'includes/theme-functions.php', 		// Custom theme functions
				'includes/theme-plugins.php', 			// Theme specific plugins integrated in a theme
				'includes/theme-actions.php', 			// Theme actions & user defined hooks
				'includes/theme-comments.php', 			// Custom comments/pingback loop
				'includes/theme-js.php', 				// Load JavaScript via wp_enqueue_script
				'includes/sidebar-init.php', 			// Initialize widgetized areas
				'includes/theme-widgets.php',			// Theme widgets
				'includes/theme-install.php',			// Theme Installation
				'includes/theme-woocommerce.php'		// WooCommerce overrides
				);

// Allow child themes/plugins to add widgets to be loaded.
$includes = apply_filters( 'woo_includes', $includes );
				
foreach ( $includes as $i ) {
	locate_template( $i, true );
}

/*-----------------------------------------------------------------------------------*/
/* You can add custom functions below */
/*-----------------------------------------------------------------------------------*/

// Create A Facebook Share Button Shortcode
function facebook_share3() {
    return '<div class="fbshare"><script src="http://widgets.fbshare.me/files/fbshare.js"></script></div>';
}
add_shortcode( 'facebook_share', 'facebook_share3' );

/* This snippet removes the action that inserts thumbnails to products in teh loop
 * and re-adds the function customized with our wrapper in it.
 * It applies to all archives with products.
 */
 
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
 
/**
 * WooCommerce Loop Product Thumbs
 **/
 
 if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
 
	function woocommerce_template_loop_product_thumbnail() {
		echo woocommerce_get_product_thumbnail();
	} 
 }
 
 
/**
 * WooCommerce Product Thumbnail
 **/
 if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
	
	function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $placeholder_width = 0, $placeholder_height = 0  ) {
		global $post, $woocommerce;
 
		if ( ! $placeholder_width )
			$placeholder_width = $woocommerce->get_image_size( 'shop_catalog_image_width' );
		if ( ! $placeholder_height )
			$placeholder_height = $woocommerce->get_image_size( 'shop_catalog_image_height' );
 
			if (get_field('productFeaturedImage', $post->ID)) {

				$output .= '<img src="'.get_field('productFeaturedImage', $post->ID).'" alt="'.get_the_title().'" title="'.get_the_title().'" width="'.$placeholder_width.'" height="'.$placeholder_height.'" />';

			} elseif ( has_post_thumbnail() ) {
				
				$output .= get_the_post_thumbnail( $post->ID, $size ); 
				
			} else {
			
				$output .= '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="' . $placeholder_width . '" height="' . $placeholder_height . '" />';
			
			}
			
			return $output;
	}
 }

// Hide standard shipping option when free shipping is available
add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );
 
/**
 *  Hide Standard Shipping option when free shipping is available
 * 
 * @param array $available_methods
 */
function hide_standard_shipping_when_free_is_available( $available_methods ) {
 
    if( isset( $available_methods['free_shipping'] ) AND isset( $available_methods['flat_rate'] ) ) {
 
        // remove standard shipping option
        unset( $available_methods['flat_rate'] );
    }
 
    return $available_methods;
}

/*-----------------------------------------------------------------------------------*/
/* This Section Sets Up The Post Count & Other Needs For The Conversion Rate Report */
/*-----------------------------------------------------------------------------------*/

function the_titlesmall($before = '', $after = '', $echo = true, $length = false) { $title = get_the_title();

	if ( $length && is_numeric($length) ) {
		$title = substr( $title, 0, $length );
	}

	if ( strlen($title)> 0 ) {
		$title = apply_filters('the_titlesmall', $before . $title . $after, $before, $after);
		if ( $echo )
			echo $title;
		else
			return $title;
	}
}

function excerpt($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt)."...";
echo $excerpt;
}

// New Short Code For Putting The Contact-Block In-Page
function contactBlock() { ?>
    <h2 class="pagesubsectionheader">Need Assistance? Ready To Order?</h2>
    <?php include '/home/silicons/public_html/wp-content/themes/woostore/page-include-contact-block.php'; ?>
<?php }
add_shortcode('contact', 'contactBlock'); 

// Create Functions For Generating & Displaying Product View Counts
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
    }
    return $count;
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

// Create And Insert The Code For WP Into The Header Of The Website
add_action( 'template_redirect', 'wp_product_views' );

function wp_product_views()
{
    if ( is_product() )
        setPostViews( get_the_ID() );
}

// Load The Help Guide Menu Tab  
require_once (TEMPLATEPATH . '/customfunctions/help-tabs.php');

// Load The Error Log Dashboard Menu Page
require_once (TEMPLATEPATH . '/customfunctions/error-log.php');

// Load The Features & Updates Log Dashboard Menu Page
require_once (TEMPLATEPATH . '/customfunctions/features-updates.php');

// Load The Product Conversion Report Dashboard Menu Page
require_once (TEMPLATEPATH . '/customfunctions/conversions-report.php');

// Add New Menus As Needed
function register_my_menus() {
  register_nav_menus(
    array(
      'sidebar-store-menu' => __( 'Sidebar Store Menu' )
    )
  );
}
add_action( 'init', 'register_my_menus' );

// Custom Featured Products ShortCode - Matt B - 2/2013
function featuredProducts() {
	$featuredproducts = new wp_Query('post_type=product&meta_key=featuredproduct&meta_value=highlight&posts_per_page=3');
		while ($featuredproducts->have_posts()) : $featuredproducts->the_post();
                    if (get_field('productFeaturedImage') != '') {
                        $image = get_field('productFeaturedImage');
                    } else {
                        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large');
			$image = $image[0];
                    }
                $list .= '<li class="product">';
		$list .= '<a href="' . get_permalink() . '">';
		$list .= '<img src="' . $image . '" alt="' . get_the_title() . '" />';
		$list .= '<h3>' . get_the_title() . '</h3>';
		$list .= '</a>';
		$list .= '<a href="' . get_permalink() . '" class="button">View Details</a>';
		$list .= '</li>';
	endwhile;
	$featuredproductstwo = new wp_Query('post_type=product&meta_key=featuredproduct&meta_value=highlight&posts_per_page=1&offset=3');
		while ($featuredproductstwo->have_posts()) : $featuredproductstwo->the_post(); 
                    if (get_field('productFeaturedImage') != '') {
                        $imagetwo = get_field('productFeaturedImage');
                    } else {
                        $imagetwo = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large');
			$imagetwo = $imagetwo[0];
                    }
		$listtwo .= '<li class="product last">';
		$listtwo .= '<a href="' . get_permalink() . '">';
		$listtwo .= '<img src="' . $imagetwo . '" alt="' . get_the_title() . '" />';
		$listtwo .= '<h3>' . get_the_title() . '</h3>';
		$listtwo .= '</a>';
		$listtwo .= '<a href="' . get_permalink() . '" class="button">View Details</a>';
		$listtwo .= '</li>';
	endwhile;
    
    wp_reset_query();
    
    return '<h2 class="pagesubsectionheader" style="margin-top:20px;">Other Customers Have Loved...</h2><ul class="products">' . $list . '' . $listtwo . '</ul>';
	
}
add_shortcode('featuredproducts', 'featuredProducts');



/*-----------------------------------------------------------------------------------*/
/* Don't add any code below here or the sky will fall down */
/*-----------------------------------------------------------------------------------*/
?>